package cn.stylefeng.guns.sys.modular.system.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.ExcelUtil;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.*;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyyCwCkpxMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.CwbbParam;
import cn.stylefeng.guns.sys.modular.system.service.*;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ckpx")
public class BtyCkpxController extends BaseController {

    private String PREFIX = "/modular/system/btyCkpx/";
    @Resource
    private BtyCkpxService ckpxService;
    @Resource
    private BtyKmmxzService kmmxzService;
    @Resource
    private BtyFzhsService fzhsService;
    @Resource
    private BtyMdpdService mdpdService;
    @Resource
    private BtyYhlsService yhlsService;
    @Resource
    private BtyPzxqService pzxqService;
    @Resource
    private BtySyfsService syfsService;

    /**
     * 跳转到仓库品项
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyCkpx.html";
    }

    /**
     * 获取仓库品项列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(CwbbParam cwbbParam) {
        //拼接查询条件
//        String beginTime = "";
//        String endTime = "";
//        if (ToolUtil.isNotEmpty(cwbbParam.getTimeLimit())) {
//            String[] split = cwbbParam.getTimeLimit().split(" - ");
//            beginTime = split[0];
//            endTime = split[1] + " 23:59:59";
//            cwbbParam.setBeginTime(beginTime);
//            cwbbParam.setEndTime(endTime);
//        }
        //获取用户的姓名
        String account = LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        Page<Map<String, Object>> list = null;
        if (account.equals("admin")|roleNames.contains("财务主管")) {
            list =this.ckpxService.list(cwbbParam);
        }else if (roleNames.contains("会计")) {
            cwbbParam.setKj(account);
            list = this.ckpxService.list(cwbbParam);
        }else if(roleNames.contains("店长报销单管理")){
            cwbbParam.setDz(account);
            list =this.ckpxService.list(cwbbParam);
        }else if (roleNames.contains("督导")) {
            cwbbParam.setDd(account);
            list = this.ckpxService.list(cwbbParam);
        }
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }
    /**
     * 获取仓库品项列表(不分页)
     */
    @RequestMapping(value = "/listExportCkpx")
    @ResponseBody
    public Object listExportYhyye(CwbbParam cwbbParam) {
        //获取用户的姓名
        String account = LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        List<BtyyCwCkpx> list = null;
        if (account.equals("admin")|roleNames.contains("财务主管")) {
            list =this.ckpxService.listExportCkpx(cwbbParam);
        }else if (roleNames.contains("会计")) {
            cwbbParam.setKj(account);
            list = this.ckpxService.listExportCkpx(cwbbParam);
        }else if(roleNames.contains("店长报销单管理")){
            cwbbParam.setDz(account);
            list =this.ckpxService.listExportCkpx(cwbbParam);
        }else if (roleNames.contains("督导")) {
            cwbbParam.setDd(account);
            list = this.ckpxService.listExportCkpx(cwbbParam);
        }
        return list;
    }

    /**
     * 导入仓库品项excel
     */
    @RequestMapping("/importCkpx")
    @ResponseBody
    public Object importCkpx(@RequestParam("file") MultipartFile file,@RequestParam(value = "dm", required = false) String dm) {
        String fileName = file.getOriginalFilename();
        JsonResult jsonResult = new JsonResult();
        Boolean result;
        try {
            result = this.ckpxService.batchImportCkpx(fileName, file,dm);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return result;
    }

    /**
     * @Title: expExcel
     * @Description: 批量导出财务报表信息
     * @param @param params
     * @param @param response
     * @return void
     */
    @RequestMapping("/expExcel")
    @ResponseBody
    public void expExcel(HttpServletResponse response,CwbbParam cwbbParam){
        ckpxService.expExcel(response,cwbbParam);
    }
}